<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :cols="2" labelWidth="100" :isSubmitBtn="false" :formType="type" @formChange="formChangeHandle" />
    <div style="text-align: center;margin: 20px 0 20px 0;">
      <el-button size="small" @click="closeHandler">取消</el-button>
      <!-- <AjaxButton size="small" type="primary" @click="saveHandle">保存</AjaxButton> -->
      <el-button size="small" @click="saveHandle" :disabled="isDisabled" type="primary">保 存</el-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { updateInfoWays } from '@/api/potenCus/basedata/knowWays';

export default {
  name: 'SourceDetail',
  mixins: [dictionary],
  props: {
    rowData: '',
    type: '',
    list: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      formList: this.createFormList(),
      upSourceList: [],
      isDisabled: false,
      saveData: null
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init(params) {
      const { type } = this;
      if (type == 'ADD') {
        // this.formList.find()
      } else if (type == 'EDIT') {
        this.fillInitData();
      }
    },
    fillInitData() {
      const { rowData, list } = this;
      this.formList.forEach(item => {
        if (item.fieldName == 'upWayId') {
          item.initialValue = list.find(x => x.value == rowData.upWayId)['text'];
        } else {
          item.initialValue = rowData[item.fieldName];
        }
      });
    },
    createFormList() {
      let { rowData, type, list } = this;
      let list1 = [
        {
          type: 'INPUT',
          label: '上级途径',
          fieldName: 'upWayId',
          initialValue: '',
          placeholder: '',
          disabled: true,
          offsetRightCols: 1,
          itemList: []
        }
      ];
      let list2 = [
        {
          type: 'INPUT',
          label: '途径名称',
          fieldName: 'wayName',
          offsetRightCols: 1,
          rules: [{ required: true, message: '请填写途径名称', trigger: 'blur' }],
          initialValue: ''
        }
      ];
      let result = [];
      if (type === 'ADD') {
        list1.find(item => item.fieldName == 'upWayId').initialValue = list.find(item => item.value == rowData.id)['text'];
      } else {
        // list1.find(item => item.fieldName=='upWayId').initialValue = list.find(item => item.value == rowData.upWayId)['text']
      }
      result = [].concat(list1, list2);
      return result;
    },
    addFunc() {
      this.isDisabled = true;
      let { rowData } = this;
      this.$refs.formPanel.SUBMIT_FORM();
      let params = [
        {
          upWayId: rowData.id,
          wayName: this.saveData.wayName
        }
      ];
      // 将新节点名称和父节点id传递到后台
      updateInfoWays(params).then(res => {
        this.isDisabled = false;
        this.$emit('close', false, true);
      });
    },
    editFunc() {
      let { rowData } = this;
      this.$refs.formPanel.SUBMIT_FORM();
      let params = [
        {
          id: rowData.id,
          wayName: this.saveData.wayName
        }
      ];
      // 将新节点名称和父节点id传递到后台
      updateInfoWays(params).then(res => {
        this.$emit('close', false, true);
      });
    },
    saveHandle() {
      this.type === 'EDIT' ? this.editFunc() : this.addFunc();
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    closeHandler() {
      this.$emit('close', false);
    }
  }
};
</script>
<style lang="less" scoped></style>
